auto.waitFor();
//time的星期,取值在0-6之间，0表示星期日
var now = new Date();
var min = now.getMinutes();
var hour = now.getHours();

var date = now.getDate();
var day = now.getDay();
var month = now.getMonth() + 1;


;
if (jieri()) {
    log('节日退出')
    exit();
}

var myFunc = require("myCommonFunction.js");



myFunc.initNetWork('7865', true);


var ding = false;

var ip = false;

var wait = false;
opreate();
// openIPSA();

function opreate() {
    now = new Date();
    min = now.getMinutes();
    hour = now.getHours();
    try {
        if (hour < 11) {
            if (min < 20) {
                sleep(Math.ceil(Math.random() * 120 + 20) * 1000);
            }
            上班打卡();
            if (!wait) {
                setTasker();
            }
        } else if (hour > 17) {
            下班打卡();
        }
        if (ding && ip) {
            weixinNotify();
            myFunc.closeWifiOrData(true);
        } else if (!wait) {
            throw 'ding：' + ding + ", ip:" + ip;
        }
    } catch (error) {
        log(error);
        myFunc.tuichu('阿里钉');
        myFunc.tuichu('iPSA');
        opreate();
    }
}

function weixinNotify() {
    try {
        home();
        myFunc.openMyApp("微信");
        myFunc.clickByText('微信', 6000);
        sleep(3000);
        myFunc.clickByText('微信', 6000);
        sleep(2000);
        click(600, 311);
        sleep(2000);
        input(random())
        sleep(1000);
        click('发送');
        if (!descContains("show you").exists()) {
            throw '发送微信通知失败';
        }
    } catch (error) {
        log(error);
        myFunc.tuichu('微信');
        weixinNotify();
    }
}


function setTasker() {
    try {
        myFunc.openMyApp("Tasker");
        var we = text('配置文件').findOne(5000);
        myFunc.clickCenter(we);

        if (text('设置排序').findOne(1000) != null) {
            sleep(500)
            myFunc.clickCenter(we);
        }

        if (myFunc.clickByTextC('从 18')) {

        } else if (myFunc.clickByText('下')) {
            myFunc.clickByTextC('从 18')
        }
        sleep(2000)
        var tt = idContains('numberpicker_input').find()[1];
        tt.click()
        var ti = min + Math.ceil(Math.random() * 5);
        tt.setText(ti)
        sleep(1000);
        //back();
    } catch (error) {
        log(error);
    }
    var words = ['配置文件'];
    myFunc.tuichuToStr(words);
}

function openAliding() {
    myFunc.openMyApp("阿里钉");
    sleep(3000);
    if ('com.alibaba.android.rimet.aliding' != currentPackage()) {
        log('桌面打开');
        home();
        sleep(1000);
        myFunc.clickByText('阿里钉');
        sleep(2000);
    }
    var msg = text('消息').findOne(2000);
    if (msg == null) {
        log('退回');
        var words = ['消息'];
        myFunc.tuichuToStr(words);
        myFunc.clickByText('消息');
    } else {
        log('消息');
        myFunc.clickCenter(msg);
    }

}

function openIPSA() {
    myFunc.openMyApp("iPSA");
    sleep(3000);
    if ('com.isoftstone.welink.ipsa' != currentPackage()) {
        log('桌面打开');
        home();
        sleep(1000);
        myFunc.clickByText('iPSA');
        sleep(2000);
    }
    var msg = text('业务').findOne(2000);
    if (msg == null) {
        log('退回');
        var words = ['业务'];
        myFunc.tuichuToStr(words);
        myFunc.clickByText('业务');
    } else {
        log('业务');
        myFunc.clickCenter(msg);
    }

}

function 上班打卡() {
    log('上班打卡');
    openAliding();

    if (hour < 9 || (hour == 9 && min <= 30)) {
        log('极速打卡');
        sleep(5000);
        ding = true;
        ipasa('签到');
    } else {
        if (myFunc.textpClick('迟到打卡')) {
            log('迟到打卡');
            ding = true;
            log('iPSA 打卡');
            ipasa('签到');
            return;
        }

        if (!myFunc.clickByText('打卡', 6000)) {

            click(960, 430);
        }
        textContains('上班').findOne(5000);
        if (text('无法打卡').findOnce() != null) {
            log('无法打卡');
            //等两分钟再打卡
            setTimeout(function () {
                log('等两分钟打卡');
                opreate();
            }, 120000);
            wait = true;
        } else if (text('上班打卡').findOne(1500) != null) {
            log('30后上班打卡');
            now = new Date();
            min = now.getMinutes();
            hour = now.getHours();
            sleep(2000);
            myFunc.textpClick('上班打卡');
            if (min > 30) {
                if (myFunc.textpClick('迟到打卡')) {
                    ding = true;
                }
            }
            log('打卡', hour, min);
            // storage.put('up', hour + "-" + min);
            ipasa('签到');
        } else if (textContains('已打卡').findOnce() != null && text('下班打卡').findOnce() != null) {
            log('上班已打卡');
            ding = true;
            ipasa('签到');

            // var shangbanTime = textContains('已打卡').findOnce().text();
            // var time = shangbanTime.split('已')[0].replace(':', '-');
            // storage.put('up', time);
        } else {


            log('不匹配');
        }

    }
}

function ipasa(text) {
    openIPSA();
    myFunc.clickByText("取消", 6000);
    myFunc.clickByText('业务');
    myFunc.textpClick('新考勤打卡');
    sleep(8000);
    log(text);

    if (myFunc.clickByText(text)) {
        log("ipsaclick2");
        sleep(2000);
        if (textContains('已' + text).findOne(5000) != null) {
            ip = true;
        }
    } else if (textContains('已' + text).findOne(5000) != null) {
        ip = true;
        log("ipsa已签到");
    } else {
        log('没找到');
    }
    sleep(1000);
}



function 下班() {

    //判断上下班时长是否大于9
    //打下班卡, 7点直接打，6点根据上班时间打 上班时间+9h 1min
    if (hour > 18) {
        log('18点后打卡');
        下班打卡();
    } else {
        //判断上下班时长是否大于9
        var upTime = null;
        if (upTime == null) {

        } else {
            upTime = upTime.split('-');
        }

    }
}

function 下班打卡() {
    openAliding();
    myFunc.textpClick('打卡', 6000);

    textContains('上班').findOne(5000);

    sleep(2000);

    now = new Date();
    min = now.getMinutes();
    hour = now.getHours();

    var shangbanTime = textContains('已打卡').findOnce().text();
    var upTime = shangbanTime.split('已')[0].split(':');
    var xiaHour = parseInt(upTime[0]) + 9;
    //小于9等到大于9小时再打卡
    if (xiaHour == hour && upTime[1] >= min) {
        var waitTime = upTime[1] - min + 1;
        log('等', waitTime);
        setTimeout(function () {
            下班打卡();
        }, waitTime * 60000);

        wait = true;
    } else {
        log('18点大于9小时打卡');
        //大于9小时
        //判断是否已打卡，打卡or更新打卡
        if (text('未打卡').findOnce() != null && text('下班打卡').findOnce() != null) {
            myFunc.textpClick('下班打卡');
            ding = true;
            ipasa('签退');
        } else if (textContains('已打卡').find().length == 2) {
            //判断是否需要更新打卡
            var ttime = textContains('已打卡').find();
            var startTime = ttime[0].text().split('已')[0].split(':');
            var endTime = ttime[1].text().split('已')[0].split(':');
            if (endTime[0] < 18 || (18 == endTime[0] && startTime[1] >= endTime[1])) {
                text('更新打卡').findOnce().click();
                text('确定').findOne(3000).click();
                ding = true;
                ipasa('签退');
            } else {
                log('已打卡，且不需要更新')
                ding = true;
                ipasa('签退');
            }
        }

    }
}


function jieri() {
    //节假日 6月3日至5日  9月10日至12日  10月1日至7日放假 10月8日、10月9日上班
    if (month == 6) {
        if (date >= 3 && date <= 5) {
            return true;
        }
    }
    if (month == 9) {
        if (date >= 10 && date <= 12) {
            return true;
        }
    }
    if (month == 10) {
        if (date >= 1 && date <= 7) {
            return true;
        }
        if (date >= 8 && date <= 9) {
            return false;
        }
    }
    if (day == 0 || day == 6) {
        return true;
    } else {
        return false;
    }
}